package engine;

import acm.program.ConsoleProgram;
import acm.util.RandomGenerator;
import role.Enemy;
import role.Player;

import javax.sound.sampled.Clip;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class Engine extends ConsoleProgram {

    public static final RandomGenerator randomGenerator = RandomGenerator.getInstance();
    public static final String GAME_FILE = "res/gameMap.txt";
    public static final int DELAY = 500;
    public static final int DELAY1 = 3000;

    private Place currPlace;    // 当前所处的地点
    ArrayList<Place> places;    // 保存所有的地点
    List<Object> list = new ArrayList<>();
    List items = new ArrayList<>(); //保存所有的道具
    boolean gameEnded;          // 玩家是否退出游戏
    Enemy enemy = new Enemy();
    Player player = new Player();
    Map map = new Map();        // 创建地图对象，用来打印地图
    int checkpoint = 2;         //当前所在关卡，checkpoint=1表示第一关
    boolean canSave = false;
    Save sa = new Save();

    String filepath = "res/one-night-in-shanghai.wav";            //文件名，只能支持wav文件
    String filepath1 = "res/请选择你的英雄.wav";
    String filepath2 = "res/身体里沉睡的野兽觉醒了.wav";
    String filepath3 = "res/大小姐驾到通通闪开.wav";
    String filepath4 = "res/来和妲己玩耍吧.wav";
    String filepath5 = "res/更快更高更强.wav";
    String filepath6 = "res/干的漂亮.wav";
    String filepath7 = "res/状态不好撤退.wav";
    String filepath8 = "res/稳住我们能赢.wav";
    String filepath9 = "res/猥琐发育别浪.wav";
    String filepath10 = "res/纳尼.wav";
    String filepath11 = "res/上去开团.wav";
    musicStuff musicObject = new musicStuff();      //创建一个音乐对象吧，我也不太清楚
//    musicStuff musicObject1 = new musicStuff();
//    musicStuff musicObject2 = new musicStuff();



    public void run() {
        if (loadGame()) {
            setUpName();
            mainLoop();
        }
    }

    /**
     * 主循环。只要玩家不选择退出，游戏就一直运行下去
     */
    private void mainLoop() {
        gameEnded = false;
        moveTo(currPlace);
        Clip clip = musicObject.playMusic(filepath);            //播放音乐,返回的clip对象可以用来播放和停止音乐

        while (!gameEnded) {

            println();
            printMap();

            if (currPlace.getName().equals("小木屋")) {
                println("请输入移动方向 || 其他选项: 存档、搜索、查看行李、播放、停止（输入\"退出\"结束游戏）");
                print("> ");
                canSave = true;
            } else {
                println("请输入移动方向 || 其他选项: 搜索、查看行李、播放、停止（输入\"退出\"结束游戏）");
                print("> ");
                canSave = false;
            }


            String direction = readLine();
            switch (direction) {
                case "东":
                    if (currPlace.getEast() != null) {
                        moveTo(currPlace.getEast());
                        playScene(currPlace);
                    } else {
                        println("那边好像无路可走了……");
                    }
                    break;
                case "南":
                    if (currPlace.getSouth() != null) {
                        moveTo(currPlace.getSouth());
                        playScene(currPlace);
                    } else {
                        println("那边好像无路可走了……");
                    }
                    break;
                case "西":
                    if (currPlace.getWest() != null) {
                        moveTo(currPlace.getWest());
                        playScene(currPlace);
                    } else {
                        println("那边好像无路可走了……");
                    }
                    break;
                case "北":
                    if (currPlace.getDanger() == 25 && repairCount == 0) {
                        repairPotition();
                    } else if (currPlace.getNorth() != null) {
                        moveTo(currPlace.getNorth());
                        playScene(currPlace);
                    } else {
                        println("那边好像无路可走了……");
                    }
                    break;
                case "退出":
                    gameEnded = true;
                    println("欢迎再来！");
                    break;
                case "搜索":
                    checkItem(currPlace);
                    break;
                case "查看行李":
                    checkPack();
                    break;
                case "查看地图":
                    checkMap(checkpoint);
                    break;
                case "播放":
                    clip.start();            //播放音乐
                    break;
                case "停止":
                    clip.stop();            //停止音乐
                    break;
                case "作弊":
                    moveTo(places.get(26));
                    break;
                case "存档":
                    if (canSave) {
                        try {
                            sa.save(player, items);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        println("此处不能存档");
                    }
                    break;

                default:
                    println("你输入的命令有误，请重新输入");
            }
        }
    }
    //检查包包里的道具
    private void checkPack() {
        println("你现在共有" + player.money + "金钱," + (int) items.get(1) + "个原素瓶，" + (int) items.get(3) + "个火焰壶。");
    }

    //查看地图
    private void checkMap(int checkpoint) {

        switch (checkpoint) {
            case 1:
                println(map.map1);
                break;
            case 2:
                println(map.map2);
                break;
            case 3:
                println(map.map3);
                break;
            default:
                println("地图参数错误");
        }
    }

    //检查地点是否有道具
    private void checkItem(Place place) {
        if (place.getItem() != null) {
            println("DEBUG2 - 你找到了一个" + place.getItem() + "！");
            switch (place.getItem()) {
                case "原素瓶": {
                    items.set(1, (int) items.get(1) + 1);
                }
            }
            place.setItem(null);

        }else {
//            println("DEBUG2 - 你什么没找到。");
        }
    }
    /**
     * 移动到一个新的地点
     * @param place 要移动到的目的地
     */
    private void moveTo(Place place) {
        println("你现在到了" + place.getName());

        println(place.getMessage());
        if (place.getItem() == null) {
//            println("DEBUG - 这里什么都没有。");
        } else {
            println("DEBUG - 这里有一个" + place.getItem());
        }

        currPlace = place;
        if (currPlace.getName().equals("商店") || currPlace.getName().equals("商会")) {
            println("欢迎来到商店，请选择购买：原素瓶 ￥100 | 火焰壶 ￥50  输入”离开“退出");
            boolean back = true;
            while (back) {
                switch (readLine()) {
                    case "原素瓶":
                        if (player.money >= 100) {
                            items.set(1, (int) items.get(1) + 1);
                            player.money -= 100;
                            println("购买成功,当前金钱：" + player.money);
                        } else {
                            println("金钱不足,当前金钱：" + player.money);
                        }
                        break;
                    case "火焰壶":
                        if (player.money >= 50) {
                            items.set(3, (int) items.get(3) + 1);
                            player.money -= 50;
                            println("购买成功,当前金钱：" + player.money);
                        } else {
                            println("金钱不足,当前金钱：" + player.money);
                        }
                        break;
                    case "离开":
                        back = false;
                        break;
                    default:
                        println("输入错误，请重新选择");
                }
            }
        }
    }

    /**
     * 读取游戏配置，如地图等
     *
     * @return 读取成功返回true，失败返回false
     */
    private boolean loadGame() {
        places = new ArrayList<>();
        items.add(0, "原素瓶");
        items.add(1, 0);
        items.add(2, "火焰壶");
        items.add(3, 0);


        try {
            Scanner scanner = new Scanner(new File(GAME_FILE));
            loadPlaces(scanner);
            loadRoutes(scanner);
            loadItem(scanner);
        } catch (FileNotFoundException e) {
            println("游戏文件读取错误！");
            return false;
        }
        return true;
    }

    /**
     * 读取道路
     *
     * @param scanner 用来读取输入的scanner对象
     */
    private void loadRoutes(Scanner scanner) {
//         读取道路
        int nRoutes = scanner.nextInt();            // 道路的数量
        String routesList = scanner.next();
        //读取所有道路
        for (int i = 0; i < nRoutes; i++) {
            int oldPlace = scanner.nextInt();
            String direction = scanner.next();
            int newPlace = scanner.nextInt();
            if (direction.equals("西")) {
                places.get(oldPlace).setWest(places.get(newPlace));
                places.get(newPlace).setEast(places.get(oldPlace));
            } else if (direction.equals("南")) {
                places.get(oldPlace).setSouth(places.get(newPlace));
                places.get(newPlace).setNorth(places.get(oldPlace));
            } else if (direction.equals("东")) {
                places.get(oldPlace).setEast(places.get(newPlace));
                places.get(newPlace).setWest(places.get(oldPlace));
            } else if (direction.equals("北")) {
                places.get(oldPlace).setNorth(places.get(newPlace));
                places.get(newPlace).setSouth(places.get(oldPlace));
            }
        }
    }

    /**
     * 读取地点
     *
     * @param scanner 用来读取输入的scanner对象
     */
    private void loadPlaces(Scanner scanner) {
        // 读取地点
        int nPlaces = scanner.nextInt();            // 地点的数量
        String placeList = scanner.next();//地点列表

        // 读取所有地点
        for (int i = 0; i < nPlaces; i++) {
            int placeNumber = scanner.nextInt();
            int dangerAmount = scanner.nextInt();
            System.out.println(dangerAmount);
            String placeName = scanner.next();//地点名称

            String Message = scanner.next();// 地点的信息
            Place place = new Place(placeName);//创建一个实例place

            place.setMessage(Message);
            place.setDangerAmount(dangerAmount);
            places.add(place);
        }

        // 设置初始地点，也就是读进来的第一个地点
        currPlace = places.get(0);
    }

    // 打印当前位置东南西北
    public void printMap() {
        String north = currPlace.getNorth() != null ? currPlace.getNorth().getName() : "虚空";
        String east = currPlace.getEast() != null ? currPlace.getEast().getName() : "虚空";
        String west = currPlace.getWest() != null ? currPlace.getWest().getName() : "虚空";
        String south = currPlace.getSouth() != null ? currPlace.getSouth().getName() : "虚空";

        println("东 " + east);
        println("南 " + south);
        println("西 " + west);
        println("北 " + north);
        println();
    }


    //读取道具
    private void loadItem(Scanner scanner) {

        int itemNumber = scanner.nextInt();//道具标号总数量
        String itemName = scanner.next();
        for (int i = 0; i < itemNumber; i++) {
            int placeNumber = scanner.nextInt();
            String item = scanner.next();
            places.get(placeNumber).setItem(item);
        }

    }

    //以下内容均来自于RPG
    private void setUpName() {

        String choice = choose("你的选择", "新的开始", "读档");
        if (choice.equals("新的开始")) {
            println("在魔王势力的扩张之下，世界陆续被征服、粉碎、碾平，危机很快就逼近这块尚未被波及的土地你，被血缘选中的命运之子，被认为是唯一能打倒魔王的人，被国王托付，踏上打倒魔王的旅程......");
            pause(DELAY1);
            print("请输入你的名字：");
            player.name = readLine();
            musicObject.playMusic(filepath1);
            pause(DELAY);
            player.role = choose("请选择角色", "战士", "游侠", "术士");
            if (player.role.equals("战士")) {
                musicObject.playMusic(filepath2);
            } else if (player.role.equals("游侠")) {
                musicObject.playMusic(filepath3);
            } else if (player.role.equals("术士")) {
                musicObject.playMusic(filepath4);
            }
            println("");
            player.initialize();
        } else {
            try {
                list = sa.read();
                player = (Player) list.get(0);
                items = (ArrayList<String>) list.get(1);
                System.out.println(player);
                System.out.println(items);
                //player = sa.read();
                currPlace = places.get(4);
                if(player.role.equals("战士")){
                    musicObject.playMusic(filepath2);
                }else if(player.role.equals("游侠")){
                    musicObject.playMusic(filepath3);
                }else if(player.role.equals("术士")){
                    musicObject.playMusic(filepath4);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 提示用户做选择，如果用户的选择无效，就让用户重新输入
     *
     * @param prompt  提示语
     * @param choices 允许的几种选择
     * @return 玩家最终的选择
     */
    public final String choose(String prompt, String... choices) {
        // 将选择用逗号连起来
        String concatenatedChoices = String.join(", ", choices);
        // 最终的提示语
        String actualPrompt = String.format("%s (%s): ", prompt, concatenatedChoices);
        // 如果玩家的输入无效，就提示玩家重新输入
        while (true) {
            print(actualPrompt);
            String userChoice = readLine();

            // 逐个对比，看是否相等
            for (String choice : choices) {
                if (userChoice.equals(choice)) {
                    return choice;
                }
            }

            println("您的选择无效，请重新输入。");
        }
    }

    /**
     * 进入/触发一个新的游戏场景
     *
     * @param currPlace 游戏场景名称
     */
    private void playScene(Place currPlace) {
        //第一个大地图生成的小怪
        if (currPlace.getDanger() > 0 & currPlace.getDanger()<=10){
            int enemyCount = randomGenerator.nextInt(1, 2);
            for (int i = 0; i < enemyCount; i++) {
                if (player.isRoleDead()) {
                    println("胜败乃兵家常事，大侠请重新来过。");
                    gameEnded = true;
                    break;
                }
                //输入小怪类型为0 代表第一个场景刷的小怪
                enemy.generateRandomEnemy(currPlace.getDanger(),0);
                println();
                println("你遇到了" + enemy.role + enemy.name + "。");
                pause(DELAY);
                battle(currPlace);
            }
        }else if (currPlace.getDanger() > 10 & currPlace.getDanger()<=20){
            int enemyCount = randomGenerator.nextInt(1, 2);
            for (int i = 0; i < enemyCount; i++) {
                if (player.isRoleDead()) {
                    println("胜败乃兵家常事，大侠请重新来过。");
                    break;
                }
                //输入小怪类型为1 代表第二个场景刷的小怪
                enemy.generateRandomEnemy(currPlace.getDanger(),1);
                println();
                println("你遇到了" + enemy.role + enemy.name + "。");
                pause(DELAY);
                battle(currPlace);
            }
        }else if (currPlace.getDanger() > 20 & currPlace.getDanger()<=30){
            int enemyCount = randomGenerator.nextInt(1, 2);
            for (int i = 0; i < enemyCount; i++) {
                if (player.isRoleDead()) {
                    println("胜败乃兵家常事，大侠请重新来过。");
                    break;
                }
                //输入小怪类型为2 代表第三个场景刷的小怪
                enemy.generateRandomEnemy(currPlace.getDanger(),2);
                println();
                println("你遇到了" + enemy.role + enemy.name + "。");
                pause(DELAY);
                battle(currPlace);
            }
        } else if(currPlace.getDanger() == 100){
            //输入boss等级 boss生成编号
            enemy.generateBoss(10,0);//色孽女巫
            println();
            println("你遇到了" + enemy.role + enemy.name + "。");
            pause(DELAY);
            battle(currPlace);
            //如果打赢了 将该地区危险度降为0
            if (player.curHp>0){
                currPlace.setDangerAmount(0);
            }
        }
        else if (currPlace.getDanger() == 101){
            //输入boss等级 boss生成编号
            enemy.generateBoss(15,1);//神尊大不敬者
            println();
            println("你遇到了" + enemy.role + enemy.name + "。");
            pause(DELAY);
            battle(currPlace);
            //如果打赢了 将该地区危险度降为0
            if (player.curHp>0){
                currPlace.setDangerAmount(0);
            }
        }  else if (currPlace.getDanger() == 102){
            //输入boss等级 boss生成编号
            enemy.generateBoss(20,2);//冰霜奇美拉
            println();
            println("你遇到了" + enemy.role + enemy.name + "。");
            pause(DELAY);
            battle(currPlace);
            //如果打赢了 将该地区危险度降为0
            if (player.curHp>0){
                currPlace.setDangerAmount(0);
            }
            }
        else if (currPlace.getDanger() == 103){
            //输入boss等级 boss生成编号
            enemy.generateBoss(20,3);//三头熔岩恶犬
            println();
            println("你遇到了" + enemy.role + enemy.name + "。");
            pause(DELAY);
            battle(currPlace);
            //如果打赢了 将该地区危险度降为0
            if (player.curHp>0){
                currPlace.setDangerAmount(0);
            }
        }
        else if (currPlace.getDanger() == 104){
            //输入boss等级 boss生成编号
            enemy.generateBoss(100,4);//魔王
            println();
            println("你遇到了" + enemy.role + enemy.name + "。");
            pause(DELAY);
            battle(currPlace);
            //如果打赢了 将该地区危险度降为0
            if (player.curHp>0){
                currPlace.setDangerAmount(0);
                println("通关嘞!");
                gameEnded = true;
            }
        }

    }


    /**
     * 玩家和敌人对战
     */
    private void battle(Place currPlace) {
        println(player);
        pause(DELAY);
        println(enemy);

        // 回合制循环进行，直至某一方阵亡
        label:
        while (true) {
            println();
            // 每一回合都首先从玩家开始行动
            String userChoice = choose("请选择你的行动", "攻击", "逃跑", "查看状态","使用技能","使用道具");

            label1:
            switch (userChoice) {
                case "查看状态":
                    println(player);
                    continue;
//                case "补血":
//                    useHealthPotion();
//                    printPlayerStatus();
//                    continue;
                case "逃跑":
                    boolean success = randomGenerator.nextBoolean();
                    musicObject.playMusic(filepath7);
                    pause(DELAY);
                    if (success) {
                        println("逃跑成功！");
                        break label;
                    } else {
                        println("逃跑失败！");
                    }
                    break;
                case "攻击":
                    if (nfootMan != 0) {
                        footMan.curHp -= player.attack(footMan);
                        println("仆从受到" + player.attack(footMan) + "点伤害");
                        println("仆从剩余血量" + footMan.curHp);
                        if (footMan.curHp <= 0) {
                            println("你杀死了仆从");
                            nfootMan--;
                        }
                        break;
                    } else {
                        attackEnemy();
                        println(enemy);
                        break;
                    }
                case "使用技能":
                    switch (player.role) {
                        case "战士":
                            skillEnemy("战士");
                            println(enemy);
                            break;
                        case "游侠":
                            skillEnemy("游侠");
                            println(enemy);
                            break;
                        case "术士":
                            skillEnemy("术士");
                            println(enemy);
                            break;
                    }
                    break;
                case "使用道具":
                    String itemChoice = choose("请输入你想使用的道具或返回上一级菜单", "原素瓶", "火焰壶", "返回");
//                    println("输入你想使用的道具");
                    switch (itemChoice) {
                        case "原素瓶":
                            useItem("原素瓶");
                            break;
                        case "炸弹":
                            useItem("火焰壶");
                            break;
                        case "返回":
                            continue ;
                        case "查看行李":
                            checkPack();
                            continue ;
//                            break label1;
                    }
            }

            if (enemy.isRoleDead()) {
                if (currPlace.getDanger() == 102 && currPlace.getDanger() == 103) {
                    fireIceDrop();
                }
                if (currPlace.getDanger()==103){
                    player.attack+=10;
                }
                // 如果敌人阵亡，玩家经验值提升
                println(String.format("你杀死了%s。\n", enemy.name));
                musicObject.playMusic(filepath6);
                pause(DELAY);
                player.money += enemy.level *10;
                println("你获得了"+enemy.level *10+"￥");
                pause(DELAY);
                println("你获得了" + player.gainXp(enemy) + "点经验值。");
                if(player.checkLevelUp()){
                    musicObject.playMusic(filepath5);
                    pause(DELAY);
                    println("你升级了！血量恢复满格！");
                }
                pause(DELAY);
                println("你当前拥有" + player.xp + "点经验值。");
                println(player);
                break;
            } else if (currPlace.getDanger() == 100) {
                witchAttack();
                if (player.isRoleDead()) {
                    musicObject.playMusic(filepath9);
                    pause(DELAY);
                    gameEnded = true;
                    println("你挂了！");
                    break;
                }

            } else if (currPlace.getDanger() == 102) {
                iceAttack();
                if (player.isRoleDead()) {
                    musicObject.playMusic(filepath9);
                    pause(DELAY);
                    gameEnded = true;
                    println("你挂了！");
                    break;
                }

            } else if (currPlace.getDanger() == 103) {
                fireAttack();
                if (player.isRoleDead()) {
                    musicObject.playMusic(filepath9);
                    pause(DELAY);
                    gameEnded = true;
                    println("你挂了！");
                    break;
                }

            } else if (currPlace.getDanger() == 104) {
                devilKing();
                if (player.isRoleDead()) {
                    musicObject.playMusic(filepath9);
                    pause(DELAY);
                    gameEnded = true;
                    println("你挂了！");
                    break;
                }

            } else {
                // 没阵亡则轮到敌人行动
                    attackPlayer();
                    pause(DELAY);
                    println(player);
                // 如果敌人将玩家打死，游戏结束
                if (player.isRoleDead()) {
                    musicObject.playMusic(filepath9);
                    pause(DELAY);
                    gameEnded = true;
                    println("你挂了！");
                    break;
                }
            }
        }
    }

    //boss战  火魔兽  特点：释放火焰，每回合给玩家造成额外伤害，并降低玩家攻击力
    int fire = 1;

    public void fireAttack() {
        player.curHp -= enemy.attack(player);
        println("你受到了" + enemy.attack(player) + "点伤害");
        println("火魔兽开始火焰吐息");
        println("火焰席卷范围很大，你无法及时躲开，靠毅力生生抗了过去");
        println("由于灼烧损失100生命");
        player.curHp -= 100;
        if (fire == 1) {
            println("燥热的环境使你心烦意乱，攻击力因此下降10点");
            player.attack -= 10;
            fire--;
        }
        println(player);

    }

    //    boss战  冰魔兽  特点：释放寒气，命中后中断玩家行动一回合
    public void iceAttack() {
        player.curHp -= enemy.attack(player);
        println("你受到了" + enemy.attack(player) + "点伤害");
        println(player);
        println("冰魔兽开始释放霜之哀伤");
        final RandomGenerator randomGenerator = RandomGenerator.getInstance();
        int judge = randomGenerator.nextInt(0, 1);
        if (judge == 1) {
            println("寒气向你席卷而来，你逃避不及，不幸被打中，暂时动弹不得");
            iceAttack();
        } else {
            println("寒气向你袭来，你奋力一跃，躲了过去");
        }

    }


    //    冰魔兽和火魔兽死亡掉落道具
    public void fireIceDrop() {
        if (enemy.name.equals("冰魔兽")) {
            println(enemy.name + "的尸体上出现了一个亮晶晶东西");
            println("你捡起了冰魔珠");
            items.add("冰魔珠");
        } else if (enemy.name.equals("火魔兽")) {
            println(enemy.name + "的尸体上出现了一个亮晶晶东西");
            println("你捡起了火魔珠");
            items.add("火魔珠");
        }

    }

    //    利用冰魔珠和火魔珠修复传送阵
    int repairCount = 0;
    public void repairPotition() {
        String a = new String();
        String b = new String();
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).equals("冰魔珠")) {
                a = "冰魔珠";
            }
            if (items.get(i).equals("火魔珠")) {
                b = "火魔珠";
            }
        }
        if (a.equals("冰魔珠") && b.equals("火魔珠")) {
            println("阵法损坏，是否取出冰魔珠和火魔珠修复阵法");
            String itemChoice = choose("请输入”是“或者”否“","是","否");
            switch (itemChoice){
                case "是":
                    println("阵法已修复，可以进行传送");
                    repairCount++;
                    break;
                case "否":
                    println("阵法损坏需要修复");
                    break;
            }
        }else {
            println("阵法损坏需要修复");
        }
    }

    //女巫  每回合会额外给玩家造成伤害或者治疗自身
    public void witchAttack() {
        player.curHp -= enemy.attack(player);
        println("你受到了" + enemy.attack(player) + "点伤害");
        final RandomGenerator randomGenerator = RandomGenerator.getInstance();
        int judge = randomGenerator.nextInt(0, 1);
        println(player);
        if (judge == 1) {
            println("女巫放出毒雾，你受到了100点伤害");
            player.curHp -= 100;
        } else {
            println("女巫对自己施展治疗术，自己血量回复10");
            if (enemy.maxHp - enemy.curHp >= 10) {
                enemy.curHp = enemy.maxHp;
                println(enemy);
            } else {
                enemy.curHp = enemy.curHp + 10;
                println(enemy);
            }
        }

    }
    //    魔王  血量低于一定值变身
    int count = 2;
    int nfootMan = 0;

    public void devilKing() {
        if (enemy.curHp <= enemy.maxHp * 0.5 && count == 2) {
            println("愤怒的魔王开始变身，大幅提升自身攻击防御");
            enemy.attack += 100;
            enemy.defense += 100;
            enemy.curHp = enemy.maxHp;
            count--;
            println(enemy);
        } else if (enemy.curHp <= enemy.maxHp * 0.5 && count == 1) {
            println("魔王召唤随从作战");
            println("出现了3只随从，随从具有嘲讽属性，你必须优先攻击随从");
            nfootMan += 3;
            creatFootMan();
            println("仆从开始攻击玩家");
            for (int i = 0; i < nfootMan; i++) {
                player.curHp -= footMan.attack(player);
            }
            println("你受到了" + footMan.attack(player)*nfootMan + "点伤害");
            count--;
        }
        println("魔王开始攻击");
        player.curHp -= enemy.attack(player);
        println("你受到了" + enemy.attack(player) + "点伤害");

    }

    //    创造魔王仆从
    Enemy footMan = new Enemy();

    public void creatFootMan() {
        footMan.maxHp = 100;
        footMan.curHp = footMan.maxHp;
        footMan.attack = 10;
        footMan.defense = 10;
    }

    //    小偷 血量低于一定值逃跑
    public void thief() {
        if (enemy.curHp <= enemy.maxHp * 0.3) {
            println("小偷见势不妙，脚底抹油跑了，你一掏口袋，少了100块钱");
            player.money -= 100;
            mainLoop();
        } else {
            player.curHp -= enemy.attack(player);
            println("你受到了" + enemy.attack(player) + "点伤害");
            println(player);
        }
    }
    /**
     * 玩家攻击敌人
     */
    private void attackEnemy() {

        int damage = player.attack(enemy);
        musicObject.playMusic(filepath11);
        pause(DELAY);

        println(String.format("%s对%s造成%d点伤害。", player.name, enemy.name, damage));
    }

    /**
     * 敌人攻击玩家
     */
    private void attackPlayer() {

        int damage = enemy.attack(player);
        musicObject.playMusic(filepath11);
        pause(DELAY);

        println(String.format("%s对%s造成%d点伤害。", enemy.name, player.name, damage));
    }

    /**
     * 使用技能攻击敌人造成的伤害
     */
    private void skillEnemy(String role) {
        int damage;
        switch (role){

            case "战士":
                damage = player.skill(enemy,"战士");
                println(String.format("%s使用『苍穹龙炎』，化身一条巨龙，吐出龙炎净化一切，对%s造成%d点伤害！", player.name, enemy.name, damage));
                pause(DELAY);
                break;
            case "游侠":
                damage = player.skill(enemy,"游侠");
                println(String.format("%s使用『射手天箭』，朝天空射出无数箭雨，对%s造成%d点伤害！", player.name, enemy.name, damage));
                pause(DELAY);
                break;
            case "术士":
                damage = player.skill(enemy,"术士");
                println(String.format("%s使用『红舞华莲』，四周土地分崩离析，裂缝中涌出无数火柱，对%s造成%d点伤害！", player.name, enemy.name, damage));
                pause(DELAY);
                break;
        }
    }

//    敌人死亡捡起道具


    private void useItem(String name) {
        switch (name) {
            case "原素瓶":
                int x = (int) items.get(1);
                if (x > 0) {
                    player.curHp = player.maxHp;
                    items.set(1, x - 1);
                    println(String.format("%s使用了原素瓶，一阵暖意在腹部流淌。恢复满血！当前血量为%d。", player.name, player.curHp));
                    pause(DELAY);
                    break;
                } else {
                    println("你没有找到原素瓶！站在原地发呆！");
                    pause(DELAY);
                    break;
                }

            case "元素瓶2":
        }

    }

    /**
     * 使用补血药剂，可以补血25。
     */

    private void useHealthPotion() {
        if (player.hpPotions > 0) {
            int hpIncreased = 25;
            if (hpIncreased > (player.maxHp - player.curHp)) {
                hpIncreased = player.maxHp - player.curHp;
            }
            player.curHp += hpIncreased;
            player.hpPotions--;
            println(String.format("成功回血%d。当前血量%d, 还剩%d瓶回血药。", hpIncreased, player.curHp, player.hpPotions));
        } else {
            println("回血失败：回血药已用完。");
        }

    }


}
